package com.ruoyi.wechat.config;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants.SignType;
import com.github.wxpay.sdk.WXPayUtil;
import com.ruoyi.common.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序预下单流程
 * 
 * @author Administrator
 *
 */
@Component
public class JsApiPreOrder {
	@Autowired
	private WechatConfig wechatConfig;
	/**
	 * 小程序预下单
	 * @param payMoney 支付金额
	 * @param orderNo 订单号
	 * @param payNo 支付单号
	 * @param notifyUrl 回调通知
	 * @return
	 */
	public Map<String, String> wxPrePay(String openId, int payMoney, String orderNo, String payNo, String notifyUrl) {
		payMoney = 1;
		
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("appid", wechatConfig.getXcxAppId());
		parameters.put("mch_id", wechatConfig.getMch_id());
		parameters.put("nonce_str", WXPayUtil.generateNonceStr());
		parameters.put("body", "支付单号:" + orderNo);
		parameters.put("out_trade_no", payNo); // 订单id
		parameters.put("fee_type", "CNY");
		parameters.put("total_fee", payMoney+"");
		parameters.put("spbill_create_ip", IpUtils.getHostIp());
		parameters.put("notify_url", wechatConfig.getDomain()+notifyUrl);
		parameters.put("trade_type", "JSAPI");
		parameters.put("openid", openId);
		
		try {
			WXPay wxpay = new WXPay(new WechatPayConfig(wechatConfig.getXcxAppId(),wechatConfig.getMch_id(),wechatConfig.getApi_key(),wechatConfig.getCertPath()));
			
			String sign = WXPayUtil.generateSignature(parameters, wechatConfig.getApi_key());
			parameters.put("sign", sign);
			// 封装请求参数结束
			// 调用统一下单接口
			Map<String, String> resp = wxpay.unifiedOrder(parameters);
			if (resp.get("return_code").equals("SUCCESS")) {
				if (resp.get("return_code").equals("SUCCESS")) {
					String timeMillis = String.valueOf(System.currentTimeMillis() / 1000);
					String packageStr = "prepay_id=" + resp.get("prepay_id");

					// 页面上的sign
					Map<String , String> paySignMap = new HashMap<>();
					paySignMap.put("appId", wechatConfig.getXcxAppId());
					paySignMap.put("nonceStr", WXPayUtil.generateNonceStr());
					paySignMap.put("package", packageStr);
					paySignMap.put("signType", SignType.MD5.name());
					paySignMap.put("timeStamp", timeMillis);
					String paySign = WXPayUtil.generateSignature(paySignMap, wechatConfig.getApi_key());
					paySignMap.put("paySign", paySign);
					
					paySignMap.put("prepay_id", resp.get("prepay_id"));
					return paySignMap;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 小程序预退款
	 * @param refund_fee 支付金额
	 * @param total_fee 订单号
	 * @param out_refund_no 支付单号
	 * @param notify_url 回调通知
	 * @param xcxAppid 小程序APPID
	 * @param mch_id 商户号
	 * @param apikey key
	 * @return
	 */
	public Map<String, String> orderRefund(int refund_fee,int total_fee, String out_refund_no,String notify_url, String transaction_id,  String xcxAppid, String mch_id, String apikey , String certP) {
		
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("appid", xcxAppid);
		parameters.put("mch_id", mch_id);
		parameters.put("nonce_str", WXPayUtil.generateNonceStr());
		parameters.put("out_refund_no", out_refund_no); // 商户退款号
		parameters.put("transaction_id", transaction_id);//微信交易号
		parameters.put("refund_fee", refund_fee+"");//退款金额
		parameters.put("total_fee", total_fee+"");//订单金额
		parameters.put("notify_url", notify_url);
		try {
			String sign = WXPayUtil.generateSignature(parameters, apikey);
			parameters.put("sign", sign);
			
			WechatPayConfig config = new WechatPayConfig(wechatConfig.getXcxAppId(),wechatConfig.getMch_id(),wechatConfig.getApi_key(),wechatConfig.getCertPath());
			WXPay wxpay = new WXPay(config);
			
			// 封装请求参数结束
			// 调用统一下单接口
			Map<String, String> resp = wxpay.refund(parameters);
			if (resp.get("return_code").equals("SUCCESS")) {
				if (resp.get("return_code").equals("SUCCESS")) {
					 Map<String , String> paySignMap = new HashMap<>();
					 paySignMap.put("refund_id", resp.get("refund_id"));
					 paySignMap.put("refund_fee", resp.get("refund_fee"));
					 paySignMap.put("out_refund_no", resp.get("out_refund_no"));
					 paySignMap.put("out_trade_no", resp.get("out_trade_no"));
					 paySignMap.put("transaction_id", resp.get("transaction_id"));
					 
					return paySignMap;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
